#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

char *MyGetString(char*pbuf,int size);

int main(int argc,char*argv[])
{
    struct sockaddr_in servaddr;
    int sockfd = -1;
    int ret = 0;
    char buf[64]="";
    int size=0;

    sockfd = socket(AF_INET,SOCK_STREAM,0);

    bzero(&servaddr,sizeof(servaddr));
    //memset(&servaddr,0,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(2345);
    inet_aton("127.0.0.1",&servaddr.sin_addr);

    ret = connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    if(ret<0)
    {
        printf("error");
        return 1;
    }
    // char ch[6]="";
    // scanf("%s",ch);

    //memset(buf,0,64);
    printf("Please your content:\n");
    MyGetString(buf,64);
    size=strlen(buf)+1;
    write(sockfd,&size,sizeof(int));
    write(sockfd,buf,size);


    //write(sockfd,"Hello",6);
    read(sockfd,buf,8);
    printf("In client,buf = %s\n",buf);
    close(sockfd);
    sockfd=-1;

    return 0;
}

char *MyGetString(char*pbuf,int size)
{
    char *p=NULL;
    int len=0;
    p = fgets(pbuf,size,stdin);
    if(NULL==p)
    {
        return NULL;
    }
    //判断用户本次输入有剩余
    len=strlen(pbuf);
    if(*(pbuf+len-1)=='\n')
    {
        *(pbuf+len-1)='\0';
    }
    else
    {
        while(getchar()!='\n')
        {

        }
    }
    return p;
}